package info.vanbeem.WikiMindDesktop;

import java.io.File;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

class EditorController {
	
	public EditorController(MainWindow mainwindow) {
		this.mainWindow = mainwindow;
		editor = mainWindow.getEditor();
		
		mainWindow.getEditor().addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				mainWindow.setFileIsChanged(true);
			}
		});
		
		mainWindow.getEditor().addListener (SWT.FocusOut, new Listener () {
			public void handleEvent (Event event) {
				save();
			}
		});
	}

	private final MainWindow mainWindow;
	private final Text editor;

	void save() {
		if ( mainWindow.isFileIsChanged() )
		{
			File file = mainWindow.getActiveFile();
			if ( file != null ) {
				try {
					String newtext = editor.getText();
					Utils.writeFile(file, newtext);
					mainWindow.setFileIsChanged(false);
					mainWindow.getFileViewer().setText(newtext);
				} catch (IOException e) {
					// TODO Auto-generated catch block
				}
			}
		}
	}

	void setEnabled(final boolean enabled) {
		editor.setEnabled(enabled);
	}

	void setText(final String text) {
		editor.setText(text);
		mainWindow.setFileIsChanged(false);
	}
	
	int startOfLine(StringBuilder line, String linedelimiter, int startpos) {
		startpos = startpos - linedelimiter.length();
		while (startpos > 0 && line.substring(startpos, startpos + linedelimiter.length()).compareTo(linedelimiter) != 0) { 
			startpos--;
		}
		return startpos > 0 ? startpos + linedelimiter.length() : 0;
	}

	int endOfLine(StringBuilder line, String linedelimiter, int endpos) {
		while ( endpos < line.length() - linedelimiter.length() && 
				line.substring(endpos, endpos + linedelimiter.length()).compareTo(linedelimiter) != 0) {
			endpos++;
		}
		return endpos;
	}
	
	void setHeading(int level) {
		final StringBuilder buf = new StringBuilder(editor.getText());
		final String linedelimiter = editor.getLineDelimiter();
		int caretpos = editor.getCaretPosition();
		int startpos = startOfLine(buf, linedelimiter, caretpos);
		int endpos = endOfLine(buf, linedelimiter, caretpos);
		
		StringBuilder line = new StringBuilder(buf.substring(startpos, endpos).trim());
		while (line.length() > 0 && line.charAt(0) == '=') {
			line.delete(0, 1);
		}
		while (line.length() > 0 && line.charAt(line.length() -1) == '=') {
			line.delete(line.length() -1, line.length());
		}
		for ( int cnt = level; cnt > 0; cnt-- ) {
			line.insert(0, '=');
			line.append('=');
		}
		buf.replace(startpos, endpos, line.toString());
		editor.setText(buf.toString());
		editor.setSelection(startpos + line.length() - level);
	}
	
	void setFont(final String decorator) {
		
	}
	
	void setEnumeration(final String prefix) {
		
	}
}
